package utils

import (
"fmt"
	"gin_blog/model"
	"github.com/gin-gonic/gin"
	"os"
	"reflect"
)

func GetUUID()string {
	f, _ := os.OpenFile("/dev/urandom", os.O_RDONLY, 0)
	b := make([]byte, 16)
	f.Read(b)
	f.Close()
	uuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
	fmt.Println(uuid)
	return uuid
}

func GetUserId(ctx *gin.Context)int{
	var tokenModel model.UserTokenModel

	//获取数据库对象
	db := model.GetDB()
	//获取用户信息
	token := ctx.Query("token")
	fmt.Println("token:", token)

	//根据用户查userid
	db.First(&tokenModel, "token=?", token) //指定根据name去查询用户的id
	fmt.Println("tokenModel.UserId", tokenModel.UserId)
	fmt.Println("tokenModel.Token", reflect.TypeOf(tokenModel.UserId))

	return tokenModel.UserId
}